package com.bifang.core.excel.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.common.annotations.ApiService;
import com.bifang.common.annotations.OpApi;
import com.bifang.common.constant.enums.common.BoolEnum;
import com.bifang.common.constant.enums.common.FuncTypeEnum;
import com.bifang.common.exception.AppException;
import com.bifang.common.vo.ExcelFieldVO;
import com.bifang.common.vo.SelectVO;
import com.bifang.common.vo.excel.ExcelModelCodeVO;
import com.bifang.common.vo.excel.ExcelModelDetailVO;
import com.bifang.common.vo.excel.ExcelModelQueryVO;
import com.bifang.common.vo.excel.ExcelModelVO;
import com.bifang.common.vo.excel.ExcelParamDelVO;
import com.bifang.common.vo.excel.ExcelParamVO;
import com.bifang.core.dao.model.ExcelModel;
import com.bifang.core.dao.model.ExcelParam;
import com.bifang.core.dao.service.ExcelModelService;
import com.bifang.core.dao.service.ExcelParamService;
import com.bifang.core.dict.integration.DictIntegration;
import com.bifang.core.excel.integration.ExcelIntegration;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
@ApiService(funcCode = "EXCEL", title = "excel表格服务")
public class ExcelIntegrationImpl implements ExcelIntegration {
    @Autowired ExcelModelService excelModelService;
    @Autowired ExcelParamService excelParamService;
    @Autowired DictIntegration dictIntegration;

    @Override
    @OpApi(
            funcCode = "EXCEL001",
            title = "根据id查询excel模板详情",
            funcType = FuncTypeEnum.query,
            publicFlag = BoolEnum.TRUE,
            savelog = false)
    public ExcelModelVO queryDetail(ExcelModelDetailVO vo) {
        return this.queryById(vo.getId());
    }

    @Override
    public ExcelModelVO queryById(String id) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL002",
            title = "根据模板编号和版本查询excel模板详情",
            funcType = FuncTypeEnum.query,
            publicFlag = BoolEnum.TRUE,
            savelog = false)
    public ExcelModelVO queryByCode(ExcelModelCodeVO vo) {
        return this.queryByCode(vo.getModelCode(), vo.getVersion(), vo.getDictMap());
    }

    @Override
    public ExcelModelVO queryByCode(String modelCode, String vesion) {
        return this.queryByCode(modelCode, vesion, null);
    }

    @Override
    public ExcelModelVO queryByCode(
            String modelCode, String version, Map<String, List<SelectVO>> dictMap) {
        if (StringUtils.isBlank(modelCode)) {
            throw new AppException("模板编号不能为空");
        }
        QueryWrapper<ExcelModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExcelModel::getModelCode, modelCode);
        if (StringUtils.isBlank(version)) {
            queryWrapper.lambda().eq(ExcelModel::getNewFlag, BoolEnum.TRUE.getType());
        } else {
            queryWrapper.lambda().eq(ExcelModel::getVersion, version);
        }
        ExcelModel excelModel = excelModelService.getOne(queryWrapper);
        if (excelModel == null) {
            throw new AppException("未找到excel模板信息");
        }
        ExcelModelVO excelModelVO = Convert.convert(ExcelModelVO.class, excelModel);
        // 获取字段信息
        List<ExcelParamVO> paramVOList = this.getByModelId(excelModelVO.getId());
        for (ExcelParamVO excelParamVO : paramVOList) {
            if (excelParamVO.getHeaderBgColor() == null) {
                excelParamVO.setHeaderBgColor(excelModelVO.getHeaderBgColor());
            }
            if (excelParamVO.getHeaderColor() == null) {
                excelParamVO.setHeaderColor(excelModelVO.getHeaderColor());
            }
            if (excelParamVO.getBodyBgColor() == null) {
                excelParamVO.setBodyBgColor(excelModelVO.getBodyBgColor());
            }
            if (excelParamVO.getBodyColor() == null) {
                excelParamVO.setBodyColor(excelModelVO.getBodyColor());
            }
            if (StringUtils.isBlank(excelParamVO.getWrapText())) {
                excelParamVO.setWrapText(excelModelVO.getWrapText());
            }
            if (StringUtils.isBlank(excelParamVO.getFontName())) {
                excelParamVO.setFontName(excelModelVO.getFontName());
            }
            if (excelParamVO.getFontSize() == null) {
                excelParamVO.setFontSize(excelModelVO.getFontSize());
            }
            if (excelParamVO.getBorderSize() == null) {
                excelParamVO.setBorderSize(excelModelVO.getBorderSize());
            }
            if (excelParamVO.getBorderColor() == null) {
                excelParamVO.setBorderColor(excelModelVO.getBorderColor());
            }
            if (ExcelFieldVO.CellTypeEnum.SELECT.getType().equals(excelParamVO.getCellType())
                    || ExcelFieldVO.CellTypeEnum.MULTIPLESELECT
                            .getType()
                            .equals(excelParamVO.getCellType())) {
                String dictField = excelParamVO.getDictField();
                if (StringUtils.isBlank(dictField)) {
                    dictField = excelParamVO.getKey();
                }
                if (dictMap != null && dictMap.containsKey(dictField)) {
                    excelParamVO.setCodeList(dictMap.get(dictField));
                } else {
                    List<SelectVO> selectList = dictIntegration.getDictByDictField(dictField);
                    excelParamVO.setCodeList(selectList);
                }
            }
        }
        excelModelVO.setParamVOList(paramVOList);
        return excelModelVO;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL003",
            title = "根据条件查询excel模板列表",
            funcType = FuncTypeEnum.query,
            checkPrivilege = BoolEnum.FALSE,
            publicFlag = BoolEnum.TRUE,
            savelog = false)
    public PageInfo<ExcelModelVO> queryList(ExcelModelQueryVO vo) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL004",
            title = "新增excel模板",
            funcType = FuncTypeEnum.insert,
            publicFlag = BoolEnum.TRUE)
    public ExcelModelVO addModel(ExcelModelVO vo) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL005",
            title = "修改excel模板",
            funcType = FuncTypeEnum.edit,
            publicFlag = BoolEnum.TRUE)
    public ExcelModelVO editModel(ExcelModelVO vo) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL006",
            title = "删除excel模板",
            funcType = FuncTypeEnum.delete,
            publicFlag = BoolEnum.TRUE)
    public void delModel(ExcelModelDetailVO vo) {}

    @Override
    public List<ExcelParamVO> getByModelId(String excelModelId) {
        QueryWrapper<ExcelParam> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ExcelParam::getExcelModelId, excelModelId)
                .orderByAsc(ExcelParam::getOrderNo)
                .orderByAsc(ExcelParam::getCreateTime);
        List<ExcelParam> list = excelParamService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<ExcelParamVO> voList = Convert.toList(ExcelParamVO.class, list);
        return voList;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL008",
            title = "新增excel字段",
            funcType = FuncTypeEnum.insert,
            publicFlag = BoolEnum.TRUE)
    public ExcelParamVO addParam(ExcelParamVO vo) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL009",
            title = "修改excel字段",
            funcType = FuncTypeEnum.edit,
            publicFlag = BoolEnum.TRUE)
    public ExcelParamVO editParam(ExcelParamVO vo) {
        return null;
    }

    @Override
    @OpApi(
            funcCode = "EXCEL010",
            title = "删除excel字段",
            funcType = FuncTypeEnum.delete,
            publicFlag = BoolEnum.TRUE)
    public void delParam(ExcelParamDelVO vo) {}

    @Override
    @OpApi(
            funcCode = "EXCEL011",
            title = "根据excel模板编号查询所有版本",
            funcType = FuncTypeEnum.query,
            publicFlag = BoolEnum.TRUE,
            savelog = false)
    public List<ExcelModelVO> queryListByCode(ExcelModelCodeVO vo) {
        return null;
    }
}
